/*
 * FXGL - JavaFX Game Library. The MIT License (MIT).
 * Copyright (c) AlmasB (almaslvl@gmail.com).
 * See LICENSE for details.
 */

package com.windea.study.kotlin.fxgl.sample.sandbox

import com.almasb.fxgl.animation.*
import com.almasb.fxgl.app.*
import com.almasb.fxgl.core.math.*
import com.almasb.fxgl.dsl.*
import com.almasb.fxgl.dsl.FXGL.Companion.despawnWithScale
import com.almasb.fxgl.dsl.FXGL.Companion.spawnWithScale
import com.almasb.fxgl.dsl.components.*
import javafx.event.*
import javafx.geometry.*
import javafx.scene.input.*
import javafx.scene.paint.*
import javafx.util.*

/**
 * @author Almas Baimagambetov (almaslvl@gmail.com)
 */
class CrystalApp : GameApplication() {
	private enum class Type {
		PLAYER, CRYSTAL
	}
	
	override fun initSettings(settings: GameSettings) {
		settings.title = "Crystal Chase"
	}
	
	override fun initGame() {
		getGameScene().setBackgroundColor(Color.BLACK)
		
		run({ this.spawnCrystal() }, Duration.seconds(2.0))
	}
	
	private fun spawnCrystal() {
		
		val e = entityBuilder().at(FXGLMath.randomPoint(Rectangle2D(0.0, 0.0, (getAppWidth() - 55).toDouble(), (getAppHeight() - 55).toDouble())))
			.type(Type.CRYSTAL)
			.view(texture("YellowCrystal.png").toAnimatedTexture(8, Duration.seconds(0.66)).loop())
			.with(DraggableComponent())
			.build()
		
		e.viewComponent.addEventHandler(MouseEvent.MOUSE_CLICKED, EventHandler {
			despawnWithScale(e, Duration.seconds(1.0), Interpolators.ELASTIC.EASE_IN())
		})
		
		spawnWithScale(e, Duration.seconds(1.0), Interpolators.ELASTIC.EASE_OUT())
	}
}

fun main(args: Array<String>) {
	GameApplication.launch(CrystalApp::class.java, args)
}

